package com.egg.ylt.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.DateUtils;
import com.egg.ylt.activity.ACT_SelectTime;
import com.egg.ylt.activity.ACT_ShopAppointment;
import com.egg.ylt.adapter.ADA_SelectTimeRecycler;
import com.egg.ylt.pojo.AppointmentTimeEntity;
import com.egg.ylt.presenter.impl.SelectTimePresenterImpl;
import com.egg.ylt.view.ISelectTimeView;
import com.yonyou.framework.library.base.BaseFragment;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.AppSharedPreferences;
import com.yonyou.framework.library.common.utils.ListUtil;
import com.yonyou.framework.library.eventbus.EventCenter;

import java.util.List;

import butterknife.BindView;


/**
 * Created by sichaoqi on 2018/10/12.
 */

public class FRA_SelectTime extends BaseFragment<SelectTimePresenterImpl> implements ISelectTimeView {


    @BindView(R.id.recycler_time)
    RecyclerView recyclerTime;
    @BindView(R.id.target_view_ll)
    LinearLayout llTargetView;

    private static final String ARG_POSITION = "position";

    protected int position;
    private String mTime;
    private ADA_SelectTimeRecycler mTimeAdapter;
    private String mSelectedTime;
    private String teacherId;
    private String teacherName;

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected View getLoadingTargetView() {
        return llTargetView;
    }

    @Override
    protected void initViewsAndEvents() {
        //初始化view
        initView();

    }

    private void initView() {
        //获取当前fragment的下标
        position = getArguments().getInt(ARG_POSITION);
        //获取七天日期格式 2018-10-21
        List<String> titles = DateUtils.get7date();
        //当前日期
        mTime = titles.get(position);

        //设置recycleView的布局管理器gridView
        recyclerTime.setLayoutManager(new GridLayoutManager(mContext, 4));
        //设置适配器
        mTimeAdapter = new ADA_SelectTimeRecycler(mContext);
        recyclerTime.setAdapter(mTimeAdapter);

        //item点击监听事件
        mTimeAdapter.setOnItemClickListener(new ADA_SelectTimeRecycler.OnItemClickListener() {

            @Override
            public void onClick(int position, String time, String id, String name) {
                mSelectedTime = mTime + " " + time;
                teacherId = id;
                teacherName = name;
            }
        });
        String shopId = ACT_ShopAppointment.sShopId;
        //请求当天服务时间
        mPresenter.getServiceTime(Constants.TOKEN, ACT_SelectTime.serviceId, shopId, mTime);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fra_select_time;
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    public static FRA_SelectTime newInstance(int position) {
        FRA_SelectTime f = new FRA_SelectTime();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION, position);
        f.setArguments(b);
        return f;
    }

    @Override
    public void getTimeData(AppointmentTimeEntity entity) {
        if (entity == null || ListUtil.isListEmpty(entity.getList())) {
            toggleShowEmpty(true, R.drawable.empty_no_response, "没有可预约技师~");
            return;
        }

        toggleShowEmpty(false, -1, "");

        // 设置数据
        mTimeAdapter.setmList(entity.getList());
    }

    @Override
    public void getFailure() {
        toggleShowEmpty(true, R.drawable.empty_no_response, "没有可预约技师~");
    }

    public String getSelectedTime() {
        return mSelectedTime;
    }

    public String getTeacherIde() {
        return teacherId;
    }

    public String getTeacherName() {
        return teacherName;
    }
}
